// pages/buyTicket/buyTicket.js
Page({

  data: {
    isHst: 0, 
    seatType: 0,
    inventor: 0,
    countt: 1,
    sourceTtime: '',
    dstTime: '',
    date: '',
    shour: 0,
    dhour: 0,
    ssecnod: 0,
    dsecnod: 0
  },

  onLoad: function (options) {

  },

  onShow: function () {
    wx.request({
      url: 'http://localhost:8080/TrainManager/SelectTicket',
      data: {
        trainID: getApp().globalData.id,
        flag: 1
      },

      success: (res) => {
      
        this.setData({
          massage: res.data,
          inventor: res.data[0].inventor,
          // 截取时 分 在页面中进行到达时间的显示
          sourceTtime: res.data[0].sourcetime.substring(10, 16),
          dstTime: res.data[0].dsttime.substring(10, 16),
          strCost: res.data[0].strCost,
          date: getApp().globalData.datee
        })
      }
    })

    // 判断当前为高铁还是火车 来显示不同座位类型
    var trainID = getApp().globalData.id;
    if(trainID[0] == 'G' || trainID[0] == 'D') {
      this.setData({
        isHst: 1
      })
    }
    else {
      this.setData({
        isHst: 0
      })
    }
  },

  // 当选择座位时 设置座位id
  chooseY: function(e) {
    var id = e.currentTarget.dataset.id;  //获取自定义的ID值
    this.setData({
      seatType: id,
      id: id
    })
  },

  seatNumberInput: function(e) {
    this.setData({
      seatNumber: e.detail.value
    })
  },

  ticketCountInput: function(e) {
    this.setData({
      countt: e.detail.value
    })
  },

  add: function(e) {
    this.setData({
      countt: this.data.countt + 1
    })
  },

  des: function(e) {
    if (this.data.countt <= 1) {
      this.data.countt = 1;
    }
    else {
      this.setData({
        countt: this.data.countt - 1
      })
    }
  },

  submitOrder: function() {

    if(this.data.inventor <= 0) {
      wx.showToast({
        title: '无票',
        image: '/images/x1.png'
      })
    }
    else {
      if(this.data.seatNumber == null) {
        wx.showToast({
          title: '请输入座位号',
          image: '/images/x1.png'
        })
      }
      else if(this.data.seatType == 0) {
        wx.showToast({
          title: '请选择座位类型',
          image: '/images/x1.png'
        })
      }
      else if(this.data.countt <= 0) {
        wx.showToast({
          title: '请选择购票张数',
          image: '/images/x1.png'
        })
      }
      else {
        wx.request({
          url: 'http://localhost:8080/TrainManager/BuyTicket',
          data: {
            userID: getApp().globalData.userID,
            trainID: getApp().globalData.id,
            seatType: this.data.seatType,
            seatNumber: this.data.seatNumber,
            countt: this.data.countt,
            inventor: this.data.inventor
          },
    
          success: (res) => {
            wx.navigateTo({
              url: '/pages/buyTicketSuccess/buyTicketSuccess'
            })
          }
        })
      }
    }
  }
})